<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 组织架构相关Controller v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace app\core\controller\enterprise\org;

use app\core\controller\enterprise\EnterpriseBaseController;
use app\core\model\EnterpriseDept;

class DeptController extends EnterpriseBaseController {


    public function __construct() {
        parent::__construct();

        $this->_checkEnterpriseAdmin();
    }


    /**
     * 添加部门
     */
    public function add() {
        $title = input("post.title");
        $parentId = input("post.parentId");

        $ret = EnterpriseDept::add($this->eid, $title, $parentId);
        if ($ret !== false) {
            $this->success("添加成功");
        } else {
            $this->error("添加失败");
        }
    }

    /**
     * 更新部门信息
     *
     * @return \think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit() {
        if (is_submit()) {
            $id = input("post.id");
            $title = input("post.title");
            $parentId = input("post.parentId");
            $leader = input("post.leader");

            $ret = EnterpriseDept::updateById($this->eid, $id, [
                'title' => $title,
                'parent_id' => $parentId
            ]);

            if (!empty($leader)) {
                $oldLeaderUserids = EnterpriseDept::getLeaders($this->eid, $id, false);
                $newLeaderUsers = json_decode($leader, true);
                $newLeaderUsers = remap($newLeaderUsers, "id");

                foreach ($newLeaderUsers as $userid => $item) {
                    if (in_array($userid, $oldLeaderUserids)) {
                        continue;
                    }
                    //设置leader
                    EnterpriseDept::setLeader($this->eid, $id, $userid, true);
                }
                foreach ($oldLeaderUserids as $userid) {
                    if (isset($newLeaderUsers[$userid])) {
                        continue;
                    }
                    //取消leader
                    EnterpriseDept::setLeader($this->eid, $id, $userid, false);
                }
            }

            if ($ret) {
                $this->success("编辑成功");
            } else {
                $this->error("编辑失败");
            }
        } else {
            $deptid = input("get.deptid");
            $dept = EnterpriseDept::getById($this->eid, $deptid);
            $deptTree = EnterpriseDept::getTree($this->eid);

            $leaderCdu = EnterpriseDept::getLeaders($this->eid, $deptid, true);

            $this->assign("leaderCdu", json_encode($leaderCdu));
            $this->assign("dept", $dept);
            $this->assign("deptTree", json_encode($deptTree));
            return view();
        }
    }

    /**
     * 删除部门
     *
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del() {
        $deptid = input("post.deptid");
        EnterpriseDept::del($this->eid, $deptid);

        $this->success("删除成功");
    }
}
